﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Threading.Tasks;

namespace WiFiAccessPointScanner
{
    public static class FileExporter
    {
        public static void ExportXml(Session session, string folderPath, string fileNamePrefix, bool isNumberSuffix)
        {
            var fileName = ConstructFileName(session, fileNamePrefix, isNumberSuffix);
            using(FileStream fs = new FileStream(Path.Combine(folderPath, fileName + ".xml"), FileMode.Create, FileAccess.Write, FileShare.Read))
            {
                DataContractSerializer serializer = new DataContractSerializer(typeof(Session));
                serializer.WriteObject(fs, session);
            }
        }

        public static void ExportJson(Session session, string folderPath, string fileNamePrefix, bool isNumberSuffix)
        {
            var fileName = ConstructFileName(session, fileNamePrefix, isNumberSuffix);
            using (FileStream fs = new FileStream(Path.Combine(folderPath, fileName + ".json"), FileMode.Create, FileAccess.Write, FileShare.Read))
            {
                DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(Session));
                serializer.WriteObject(fs, session);
            }
        }

        public static void ExportCsv(Session session, string folderPath, string fileNamePrefix, bool isNumberSuffix)
        {
            var fileName = ConstructFileName(session, fileNamePrefix, isNumberSuffix);
            using (StreamWriter w = new StreamWriter(Path.Combine(folderPath, fileName + ".csv")))
            {
                foreach (var trace in session.SessionTraces)
                {
                    foreach(var sample in trace.SignalStrengthSamples)
                    {
                        List<string> csvLineElements = new List<string>();
                        csvLineElements.Add(trace.Position.X.ToString());
                        csvLineElements.Add(trace.Position.Y.ToString());
                        csvLineElements.Add(sample.Name);
                        csvLineElements.Add(sample.MAC);
                        csvLineElements.Add(sample.SignalQuality.ToString());
                        csvLineElements.Add(sample.SignalStrength.ToString());
                        w.WriteLine(string.Join(";", csvLineElements));
                    }
                }
            }
        }

        private static string ConstructFileName(Session session, string fileNamePrefix, bool isNumberSuffix)
        {
            string suffix = isNumberSuffix ? session.SessionNumber.ToString() :
                string.Format("{0} {1} {2}", session.SessionNumber, session.SessionTraces.First().Position.X, session.SessionTraces.First().Position.Y);
            return string.Format("{0} {1}", fileNamePrefix, suffix).Trim();
        }
    }
}
